package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.EmployeeUtils;
import com.xcity.game.item.ItemTemplate;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;
import naga.x.util.CommonUtils;
import naga.x.util.NumberUtils;

@PomeloMessage(type = Message.Type.RESPONSE)
public class EmployeeLevelUpResponse extends Response {
	
	private long id;
	private int level;
	private int exp;
	private int maxExp;
	private byte upped; // 是否升级
	private short[] attrsIndex = NumberUtils.EMPTY_SHORTS;
	private Object[] attrs = CommonUtils.EMPTY_OBJECT_ARRAY;
	private List<ExpBookBrief> items = Collections.emptyList();
	
	public static class ExpBookBrief {
		public int id;
		public int count;
		ExpBookBrief(int id, int count) {
			this.id = id;
			this.count = count;
		}
	}
	
	public EmployeeLevelUpResponse(int serial, Player player, Employee employee, int employeeOldLvl, EmployeeService service) {
		super(serial);
		this.id = employee.getId();
		this.level = employee.getLevel();
		this.exp = EmployeeUtils.getCurrentExp(employee.getExp(), employee.getTemplate().getQuality());
		this.maxExp = EmployeeUtils.getNextLvlNeedExp(employee.getLevel(), employee.getTemplate().getQuality());
		this.upped = (byte) (employee.getLevel() > employeeOldLvl ? 1 : 0);
		if (this.upped == 1) { // 升级之后属性才会成长
			CombatAttributeType[] attrTypes = CombatAttributeType.vals;
			this.attrsIndex = new short[attrTypes.length];
			this.attrs = new Object[attrTypes.length];
			for (int i = 0; i < attrTypes.length; i++) {
				CombatAttributeType type = attrTypes[i];
				this.attrsIndex[i] = (short) type.ordinal();
				this.attrs[i] = type.format(employee.getAttributeValue(type));
			}
		}
		List<ItemTemplate> expBooks = service.configure().getCommonConfigure().getExpBooks();
		if (expBooks != null && expBooks.size() > 0) {
			this.items = new ArrayList<ExpBookBrief>(expBooks.size());
			for (int i = 0; i < expBooks.size(); i++) {
				int itemId = expBooks.get(i).getId();
				int count = player.getBag().countOf(itemId);
				this.items.add(new ExpBookBrief(itemId, count));
			}
		}
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public int getMaxExp() {
		return maxExp;
	}

	public void setMaxExp(int maxExp) {
		this.maxExp = maxExp;
	}

	public byte getUpped() {
		return upped;
	}

	public void setUpped(byte upped) {
		this.upped = upped;
	}

	public short[] getAttrsIndex() {
		return attrsIndex;
	}

	public void setAttrsIndex(short[] attrsIndex) {
		this.attrsIndex = attrsIndex;
	}

	public Object[] getAttrs() {
		return attrs;
	}

	public void setAttrs(Object[] attrs) {
		this.attrs = attrs;
	}

	public List<ExpBookBrief> getItems() {
		return items;
	}

	public void setItems(List<ExpBookBrief> items) {
		this.items = items;
	}
	
}
